package com.hw.one.collect.dust.dao;

import com.hw.one.collect.dust.po.RaiseDust;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/4.
 */
@Repository(value = "raiseDustDao")
public class RaiseDustDao {

    @Resource
    private MongoTemplate mongoTemplateStandardSiteData;

    private static String tableName = "T_DATA_RAISE_DUST_MIN";


    public void saveRaiseDustData(RaiseDust raiseDust) {
        mongoTemplateStandardSiteData.insert(raiseDust, tableName +"-" + raiseDust.getMONITORTIME().substring(0, 4));
    }


    public void saveRaiseDustDataList(List<RaiseDust> raiseDust) {
        if (raiseDust.size() > 0) {
            mongoTemplateStandardSiteData.insert(raiseDust, tableName + "-" + raiseDust.get(0).getMONITORTIME().substring(0, 4));
        }
    }

    public RaiseDust findRaiseDustBySiteName(RaiseDust raiseDust) {
        Criteria criteria = Criteria.where("SITENAME").is(raiseDust.getSITENAME());
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "CREATETIME"));

        return mongoTemplateStandardSiteData.findOne(query, RaiseDust.class,
                tableName + "-" + raiseDust.getMONITORTIME().substring(0, 4));
    }
    public RaiseDust findRaiseDustBySiteCode(RaiseDust raiseDust) {
        Criteria criteria = Criteria.where("SITECODE").is(raiseDust.getSITECODE());
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "CREATETIME"));

        return mongoTemplateStandardSiteData.findOne(query, RaiseDust.class,
                tableName + "-" + raiseDust.getMONITORTIME().substring(0, 4));
    }


    public List<RaiseDust> findRaiseDustBySiteCodeAndTime(RaiseDust raiseDust) {
        Criteria criteria = Criteria.where("SITECODE").is(raiseDust.getSITECODE());
        criteria.and("MONITORTIME").is(raiseDust.getMONITORTIME());
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "CREATETIME"));

        return mongoTemplateStandardSiteData.find(query, RaiseDust.class,
                tableName + "-" + raiseDust.getMONITORTIME().substring(0, 4));
    }


    /**
     * 更新实时表数据
     * @autor 李亚坤
     * @createtime 2017年8月14日10:17:42
     * @param raiseDustList
     */
    public void updateRealRaiseDust(List<RaiseDust> raiseDustList) {
        if (null == raiseDustList || raiseDustList.size() < 1) {
            return;
        }
        List<String> siteCodeList = new ArrayList<>();
        for (RaiseDust raiseDust : raiseDustList) {
            siteCodeList.add(raiseDust.getSITECODE());
        }

        Criteria criteria = Criteria.where("SITECODE").in(siteCodeList);
        Query query = new Query(criteria);
        // 先删除
        mongoTemplateStandardSiteData.remove(query, tableName);
        // 再插入
        mongoTemplateStandardSiteData.insert(raiseDustList, tableName);


    }
}
